package com.shortvedio.content.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shortvedio.base.mapper.CommentsMapper;
import com.shortvedio.base.mapper.VideosAboutMapper;
import com.shortvedio.base.model.Result;
import com.shortvedio.base.model.po.Comments;
import com.shortvedio.base.model.po.VideosAbout;
import com.shortvedio.content.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private VideosAboutMapper videosAboutMapper;

    @Override
    @Transactional
    public Result<Long> addRootComment(Long videoId, Long userId, String content) {
        Comments comments = new Comments();
        comments.setVideoId(videoId);
        comments.setUserId(userId);
        comments.setContent(content);
        comments.setParent(0L); // 父评论ID为0，代表根评论
        comments.setCommentObject(0L); // 对象ID为0，代表根评论
        comments.setCommentTime(new Date());
        commentsMapper.insert(comments);
        Integer count = updateCommentCount(videoId, 1);
        if (count<=0){
            throw new RuntimeException("更新失败");
        }
        return Result.success(comments.getId());
    }

    private Integer updateCommentCount(Long videoId,Integer count){//更新回复数
        LambdaQueryWrapper<VideosAbout> videosAboutLambdaQueryWrapper = new LambdaQueryWrapper<>();
        videosAboutLambdaQueryWrapper.eq(VideosAbout::getVideosId,videoId);
        VideosAbout videosAbout = videosAboutMapper.selectOne(videosAboutLambdaQueryWrapper);
        videosAbout.setCommentsCount(videosAbout.getCommentsCount()+count);
        int update = videosAboutMapper.updateById(videosAbout);
        return update;
    }

    @Override
    @Transactional
    public Result addReplyComment(Long parentId, Long videoId, Long userId, String content, Long commentObject) {
        Comments comments = new Comments();
        comments.setParent(parentId);
        comments.setVideoId(videoId);
        comments.setUserId(userId);
        comments.setContent(content);
        comments.setCommentObject(commentObject);
        comments.setCommentTime(new Date());
        commentsMapper.insert(comments);
        Integer count = updateCommentCount(videoId, 1);
        if (count<=0){
            throw new RuntimeException("更新失败");
        }
        return Result.success(comments.getId());
    }

    @Override
    @Transactional
    public Result deleteComment(Long commentId, Long userId) {
        Comments comments = commentsMapper.selectById(commentId);
        if (!comments.getUserId().equals(userId)){
            return Result.error("用户身份错误");
        }
        LambdaQueryWrapper<Comments> commentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentsLambdaQueryWrapper.eq(Comments::getParent,commentId);
        int delete = commentsMapper.delete(commentsLambdaQueryWrapper);
        int deleteSelf = commentsMapper.deleteById(commentId);
        int deleteTotal = delete + deleteSelf;
        Integer count = updateCommentCount(comments.getVideoId(), -deleteTotal);
        if (count<=0){
            throw new RuntimeException("更新失败");
        }
        return Result.success("删除成功");
    }

}
